﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;

namespace QLTTTH
{
    public partial class frmThongTinTKB : DevExpress.XtraEditors.XtraForm
    {
        public frmThongTinTKB()
        {
            InitializeComponent();
        }

        public void ThietLapThongTin(ThoiKhoaBieu tk)
        {
            this.txtMaTKB.Text = tk.MaTKB.ToString();
            this.loupPhongHoc.EditValue = tk.MaPhong;
            this.loupLopHoc.EditValue = tk.MaLop;
            this.dtNgayHoc.EditValue = tk.NgayHoc;
            this.txtCaHoc.Text = tk.BuoiHoc;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Focus();
            this.Close();
        }

        private void btnXoas_Click(object sender, EventArgs e)
        {
            this.txtCaHoc.Text = "";
            this.loupPhongHoc.ResetText();
            this.loupLopHoc.ResetText();
            this.txtMaTKB.Text = "";
            this.dtNgayHoc.EditValue = DateTime.Now;
        }

        private void btnXoas_Click_1(object sender, EventArgs e)
        {
            this.txtCaHoc.Text = "";
            this.loupPhongHoc.ResetText();
            this.loupLopHoc.ResetText() ;
            this.txtMaTKB.Text = "";
            this.dtNgayHoc.EditValue = DateTime.Now;
        }



        public delegate void SendData(ThoiKhoaBieu tkb);

        public SendData sendData;


        private void btnLuu_Click(object sender, EventArgs e)
        {

            if (sendData != null)
            {
                var vResult = new ThoiKhoaBieuManager().Validate(GetThoiKhoaBieu());
                if (vResult.Valid)
                {
                    sendData(GetThoiKhoaBieu());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            //if (sendData != null)
            //{
            //    sendData(GetThoiKhoaBieu());
            //}
            //this.Close();
        }

        #region ---------- Lay thong tin thoi khoa bieu tu controls thong tin thoi khoa bieu-----------
        public ThoiKhoaBieu GetThoiKhoaBieu()
        {
            ThoiKhoaBieu tkb = new ThoiKhoaBieu();
            if (!txtMaTKB.Text.ToString().Equals(""))
            {
                tkb.MaTKB = int.Parse(txtMaTKB.Text.ToString());
            }
            tkb.MaLop = int.Parse(loupLopHoc.EditValue.ToString());
            tkb.MaPhong = int.Parse(loupPhongHoc.EditValue.ToString());
            tkb.BuoiHoc = txtCaHoc.Text;
            tkb.NgayHoc = DateTime.Parse(dtNgayHoc.Text.ToString());

            return tkb;
        }
        #endregion

        private void frmThongTinTKB_Load(object sender, EventArgs e)
        {
            var dsLH = LopHocManager.LayDanhSachLopHoc();
            if (dsLH.Success)
            {
                loupLopHoc.Properties.DataSource = dsLH.Result;
                loupLopHoc.Properties.ValueMember = "MaLop";
                loupLopHoc.Properties.DisplayMember = "TenLop";
                if (loupLopHoc.ItemIndex < 0)
                {
                    loupLopHoc.ItemIndex = 0;
                }
            }

            var dsPH = PhongHocManager.LayDanhSachPhongHoc();
            if (dsPH.Success)
            {
                loupPhongHoc.Properties.DataSource = dsPH.Result;
                loupPhongHoc.Properties.ValueMember = "MaPhong";
                loupPhongHoc.Properties.DisplayMember = "TenPhong";
                if (loupPhongHoc.ItemIndex < 0)
                {
                    loupPhongHoc.ItemIndex = 0;
                }
            }
        }

    }
}